<?php

namespace App\Http\Controllers;

use App\Helper\ApiResponseHelper;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use ShaoZeMing\AliSTS\Services\STSService;

class OssController extends Controller
{
    public function getSts()
    {
        $stsCnf = [
            /*STS配置*/
            'sts' => [
                'AccessKeyID' => env('ALIYUN_ACCESS_KEY'),
                'AccessKeySecret' => env('ALIYUN_ACCESS_SECRET'),
                'regionId' => env('ALIYUN_REGION_ID'),
                'endpoint' => env('ALIYUN_ENDPOINT'),
                'roleArn' => env('ALIYUN_ROLE_ARN'),  // 角色资源描述符，在RAM的控制台的资源详情页上可以获取
                'timeout' => '3600',  // 令牌过期时间
                'client_name' => 'client_name',  // setRoleSessionName可以不改
                // 在扮演角色(AssumeRole)时，可以附加一个授权策略，进一步限制角色的权限；
                // 详情请参考《RAM使用指南》
                // 这代表所有权限
                'policy' => [
                    'Statement' => [
                        [
                            'Action' => ["oss:*"],
                            'Effect' => 'Allow',
                            'Resource' => ["acs:oss:*:*:*"],
                        ]
                    ]
                ]
            ]
        ];

        try {

            $sts = new STSService($stsCnf);
            $res = $sts->getToken();
            return ApiResponseHelper::json($res);

        } catch (\Exception $exception) {

            return ApiResponseHelper::json([], 400, $exception->getMessage());
        }
    }

    public function uploadFile(Request $request)
    {
        $path = $request->path;
        $filename = $request->filename;
        $file = $request->file('file');

        $ossPath = $file->storeAs($path, $filename, 'oss');

        return ApiResponseHelper::json(['path' => $ossPath]);
    }
}

